<?php
namespace App\Domain;
use App\Model\Label as Model_Label;

class Label {

    /* 评价标签 */
    public function getEvaluate() {
        $key='evaluatelist';
        $list=\App\getcaches($key);
        if(!$list){
            $model = new Model_Label();
            $list = $model->getEvaluate();
            if($list){
                \App\setcaches($key,$list);
            }
        }
		
		$lang=\PhalApi\DI()->lang;
		foreach($list as $k=>$v){
			if($lang=='en'){
				$v['name']=$v['name_en'];
			}
			$list[$k]=$v;
		}

        return $list;
    }

    /* 评价 */
    public function setEvaluate($uid,$liveuid,$evaluateids) {

        $rs = array('code' => 0, 'msg' => \PhalApi\T('评价成功'), 'info' => array());
        
        $evaluate_a=array_filter( preg_split('/,|，/',$evaluateids) );
        
        if(!$evaluate_a){
            $rs['code'] = 1003;
			$rs['msg'] = \PhalApi\T('信息错误');
			return $rs;
        }
        
        $evaluate_a_nums=array_count_values($evaluate_a);
        if( count($evaluate_a_nums) != count($evaluate_a)){
            $rs['code'] = 1007;
            $rs['msg'] = \PhalApi\T('信息错误');
            return $rs;
        }
        
        
        $list_kv=[];
        $list=$this->getEvaluate();
        foreach($list as $k=>$v){
            $list_kv[$v['id']]=$v;
        }
        
        $evaluate=[];
        $evaluate_en=[];
        $evaluate_c=[];
        $evaluate_type=[];
        $isok=1;
        foreach($evaluate_a as $k=>$v){
            $evaluateinfo=$list_kv[$v];
            if(!$evaluateinfo){
                $isok=0;
                break;
            }
            
            $evaluate[]=$evaluateinfo['name'];
            $evaluate_en[]=$evaluateinfo['name_en'];
            $evaluate_c[]=$evaluateinfo['colour'];
            $evaluate_type[]=$evaluateinfo['type'];
            $type=$evaluateinfo['type'];
        }
        
        if(!$isok){
            $rs['code'] = 1004;
            $rs['msg'] = \PhalApi\T('信息错误');
            return $rs;
        }
        
        if(!$evaluate){
            $rs['code'] = 1004;
            $rs['msg'] = \PhalApi\T('信息错误');
            return $rs;
        }
        
        $type_nums=array_count_values($evaluate_type);
        
        if( count($type_nums) > 1){
            $rs['code'] = 1005;
		$rs['msg'] = \PhalApi\T('只能选择同一类型的标签');
            return $rs;
        }
        
        $data=[
            'uid'=>$uid,
            'liveuid'=>$liveuid,
            'evaluate'=>implode(',',$evaluate),
            'evaluate_en'=>implode(',',$evaluate_en),
            'evaluate_c'=>implode(',',$evaluate_c),
            'type'=>$type,
            'addtime'=>time(),
        ];
        
        $model = new Model_Label();
        $result = $model->setEvaluate($data);
        if(!$result){
            $rs['code'] = 1006;
            $rs['msg'] = \PhalApi\T('评价失败，请重试');
            return $rs;
        }
        
        $nums=count($evaluate);
        /* 更新主播好、差评总数 */
        $model->upLiveEvaluate($liveuid,$type,$nums);
        
        /* 更新单个标签数量 */
        foreach($evaluate as $k=>$v){
            $update=[
                'uid'=>$liveuid,
                'name'=>$v,
                'name_en'=>$evaluate_en[$k],
                'colour'=>$evaluate_c[$k],
                'type'=>$type,
                'nums'=>1,
            ];
            $model->updateEvaluateCount($update);
        }

        return $rs;
    }
    
    /* 评价列表 */
    public function getEvaluateList($liveuid,$p) {

        $model = new Model_Label();
        $list = $model->getEvaluateList($liveuid,$p);
		$lang=\PhalApi\DI()->lang;
        
        foreach($list as $k=>$v){
            $userinfo=\App\getUserInfo($v['uid']);
            $v['user_nickname']=$userinfo['user_nickname'];
            $v['avatar']=$userinfo['avatar'];
            
            $label=[];
            $evaluate=explode(',',$v['evaluate']);
            $evaluate_en=explode(',',$v['evaluate_en']);
            $colour=explode(',',$v['evaluate_c']);
            
            foreach($evaluate as $k1=>$v1){
				$name=$evaluate[$k1];
				if($lang=='en'){
					$name=$evaluate_en[$k1];
				}
				
                $label[]=[
                    'name'=>$name,
                    'colour'=>$colour[$k1],
                ];
            }
            
            $v['label']=$label;
            unset($v['id']);
            unset($v['liveuid']);
            unset($v['evaluate']);
            unset($v['evaluate_en']);
            unset($v['evaluate_c']);
            unset($v['type']);
            unset($v['addtime']);
            
            $list[$k]=$v;
        }

        return $list;
    }   

    /* 评价统计 */
    public function getEvaluateCount($liveuid) {

        $model = new Model_Label();
        $list = $model->getEvaluateCount($liveuid);
		
		$lang=\PhalApi\DI()->lang;
		foreach($list as $k=>$v){
			if($lang=='en'){
				$v['name']=$v['name_en'];
			}
			$list[$k]=$v;
		}
		
		

        return $list;
    }   

}
